package easyway.trainapp.webframework.util;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * 根据键找资源文件中的值
 * @author caiwei
 */

public class MessageFactory extends Object {
	
	//mini构造函数
	private MessageFactory() {
	}
	
	/*
	 * 以下是返回值为FacesMessage或字符串的方法。
	 */ 
	
	
	// 从资源包中获取信息，从而构造jsf页面标记<h:message>与<h:messages>中所要显示的详细和摘要信息
	public static FacesMessage getMessage(String messageId, Object params[]) {
		Locale locale = null;
		FacesContext context = FacesContext.getCurrentInstance();
		//判断资源文件目录，得到资源文件。 
		if (context != null && context.getViewRoot() != null) {
			locale = context.getViewRoot().getLocale();
			if (locale == null) {
				locale = Locale.getDefault();
			}
		} else {
			locale = Locale.getDefault();
		}

		return getMessage(locale, messageId, params);
	}

	//根据资源文件、信息摘要、信息参数来得到信息(FacesMessage)
	public static FacesMessage getMessage(Locale locale, String messageId,
			Object params[]) {
		FacesMessage result = null;
		String summary = null, detail = null, bundleName = null;
		ResourceBundle bundle = null;
		if (null != (bundleName = getApplication().getMessageBundle())) {// 资源包的名称
			if (null != (bundle = ResourceBundle.getBundle(bundleName, locale,
					getCurrentLoader(bundleName)))) {// 获取资源包
				try {
					summary = bundle.getString(messageId);// 从资源包中获取信息摘要
				} catch (MissingResourceException e) {
				}
			}
		}
		// 如果不能在用户提供的资源包中获取信息摘要，再从默认的资源包中查找
		if (null == summary) {
			bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES,
					locale, getCurrentLoader(bundleName));
			if (null == bundle) {
				throw new NullPointerException(" bundle " + bundle);
			}
			try {
				summary = bundle.getString(messageId);
			} catch (MissingResourceException e) {
			}
		}

		// 找不到对应的消息，返回null
		if (null == summary) {
			return null;
		}
		if (null == summary || null == bundle) {
			throw new NullPointerException(" summary " + summary + " bundle "
					+ bundle);
		}
		// 参数替换，用参数params替换摘要中的{0},{1}等占位符。
		summary = substituteParams(locale, summary, params);

		try {
			// 详细信息
			detail = substituteParams(locale, bundle.getString(messageId
					+ "_detail"), params);
		} catch (MissingResourceException e) {
		}
			return (new FacesMessage(summary, detail));
	}
	
	
	//得到一个不含参数的FacesMessage
	public static FacesMessage getMessage(FacesContext context, String messageId) {
		return getMessage(context, messageId, null);
	}
	//得到一个参数为字符串数组的FacesMessage
	public static FacesMessage getMessage(FacesContext context,
			String messageId, Object params[]) {
		if (context == null || messageId == null) {
			
			throw new NullPointerException(" context " + context
					+ " messageId " + messageId);
		}
		Locale locale = null;
		// 目录指向的
		if (context != null && context.getViewRoot() != null) {
			locale = context.getViewRoot().getLocale();
		} else {
			locale = Locale.getDefault();
		}
		if (null == locale) {
			throw new NullPointerException(" locale " + locale);
		}
		FacesMessage message = getMessage(locale, messageId, params);
		if (message != null) {
			return message;
		}
		locale = Locale.getDefault();
		return (getMessage(locale, messageId, params));
	}
	//得到带1个参数的FacesMessage
	public static FacesMessage getMessage(FacesContext context,
			String messageId, Object param0) {
		return getMessage(context, messageId, new Object[] { param0 });
	}
	//得到带2个参数的FacesMessage
	public static FacesMessage getMessage(FacesContext context,
			String messageId, Object param0, Object param1) {
		return getMessage(context, messageId, new Object[] { param0, param1 });
	}
	//得到带3个参数的FacesMessage
	public static FacesMessage getMessage(FacesContext context,
			String messageId, Object param0, Object param1, Object param2) {
		return getMessage(context, messageId, new Object[] { param0, param1,
				param2 });
	}
    //得到带4个参数的消息
	public static FacesMessage getMessage(FacesContext context,
			String messageId, Object param0, Object param1, Object param2,
			Object param3) {
		return getMessage(context, messageId, new Object[] { param0, param1,
				param2, param3 });
	}
	//根据资源文件、信息摘要、信息参数来得到信息字符串
	public static String getMessageString(Locale locale, String messageId
			){
		String summary = null, bundleName = null;
		ResourceBundle bundle = null;
		if (null != (bundleName = getApplication().getMessageBundle())) {// 资源包的名称
			if (null != (bundle = ResourceBundle.getBundle(bundleName, locale,
					getCurrentLoader(bundleName)))) {// 获取资源包
				try {
					summary = bundle.getString(messageId);// 从资源包中获取信息摘要
				} catch (MissingResourceException e) {
				}
			}
		}
		// 如果不能在用户提供的资源包中获取信息摘要，再从默认的资源包中查找
		if (null == summary) {
			bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES,
					locale, getCurrentLoader(bundleName));
			if (null == bundle) {
				throw new NullPointerException(" bundle " + bundle);
			}
			try {
				summary = bundle.getString(messageId);
			} catch (MissingResourceException e) {
			}
		}

		// 找不到对应的消息，返回null
		if (null == summary) {
			return null;
		}
		if (null == summary || null == bundle) {
			throw new NullPointerException(" summary " + summary + " bundle "
					+ bundle);
		}
	
		return summary;
	}
	
	//得到信息字符串
	public static String getMessageString(FacesContext context,String messageId){
		if (context == null || messageId == null) {
			throw new NullPointerException(" context " + context
					+ " messageId " + messageId);
		}
		Locale locale = null;
		// 目录指向的
		if (context != null && context.getViewRoot() != null) {
			locale = context.getViewRoot().getLocale();
		} else {
			locale = Locale.getDefault();
		}
		if (null == locale) {
			throw new NullPointerException(" locale " + locale);
		}
		locale = Locale.getDefault();
		return getMessageString(locale,messageId);
	}
	
	//获取application
	protected static Application getApplication() {
		return (FacesContext.getCurrentInstance().getApplication());
	}

	// 获取当前的类加载器，需要该类加载器来查找资源包
	protected static ClassLoader getCurrentLoader(Object fallbackClass) {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if (loader == null) {
			loader = fallbackClass.getClass().getClassLoader();
		}
		return loader;
	}
	// 把参数替换进消息字符窜中
	public static String substituteParams(Locale locale, String msgtext,
			Object params[]) {
		String localizedStr = null;

		if (params == null || msgtext == null) {
			return msgtext;
		}
		StringBuffer b = new StringBuffer(100);
		MessageFormat mf = new MessageFormat(msgtext);
		if (locale != null) {
			mf.setLocale(locale);
			b.append(mf.format(params));
			localizedStr = b.toString();
		}
		return localizedStr;
	}
	
} //end of class MessageFactory 
